﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Communication
{
    public class DataObservableDict<TKey, TValue> : INotifyPropertyChanged
    {
        public object lock_obj = new object();

        private Dictionary<TKey, TValue> _innerDictionary = new Dictionary<TKey, TValue>();  
        private List<string> _readkeys = new List<string>();

        public List<string> ReadKeys
        {
            get
            {
                return _readkeys;
            }
            set
            {
                _readkeys = value;
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public TValue this[TKey key]
        {
            get { return _innerDictionary[key]; }
            set
            {

                if (!_innerDictionary.ContainsKey(key) || !object.Equals(_innerDictionary[key], value))
                {
                    _innerDictionary[key] = value; 
                    OnPropertyChanged(key.ToString());
                }
             

            }
        }

        public ICollection<TKey> Keys => _innerDictionary.Keys;

        public ICollection<TValue> Values => _innerDictionary.Values;

        public int Count => _innerDictionary.Count;

        public void Clear()
        {
            _innerDictionary.Clear();
        } 
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
